// ts直接引js会报错，ts不知如何使用，*.d.ts(js的翻译文件)
import fs from 'fs'
import path from 'path'
import superagent from 'superagent'

export interface Analyzer {
  analyze: (rawHtml: string, filePath: string) => string
}
export class Crawler {
  private readonly filePath: string
  private async getRawHtml() {
    const response = await superagent.get(this.url)
    return response.text
  }
  private writeFile(fileContent: string) {
    fs.writeFileSync(this.filePath, fileContent)
  }
  private async initSpiderProcess() {
    // 获取html内容
    const rawHtml = await this.getRawHtml()
    // 获取需要写入文件的内容
    const fileContent = this.analyzer.analyze(rawHtml, this.filePath)
    // 写入文件
    this.writeFile(fileContent)
  }
  constructor(
    private analyzer: Analyzer,
    private url: string,
    private fileAddress: string
  ) {
    this.filePath = path.resolve(__dirname, fileAddress)
    // 开始
    this.initSpiderProcess()
  }
}
